package com.spirit.mypopup.menu;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.mec.mmdealer.R;
import com.mec.mmdealer.config.WatermarkTransformation;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @Author: wangyb on 2016/8/27 14:38.
 * @描述 : Glide加载图片的封装，圆形、圆角，模糊等处理操作用到了jp.wasabeef:glide-transformations:2.0.1
 * Glide默认使用httpurlconnection协议，可以配置为OkHttp
 */
public class GlideUtil {
    private static final String TAG = "GlideUtil";
    private static GlideUtil mInstance;


    private GlideUtil() {
    }

    public static GlideUtil getInstance() {
        if (mInstance == null) {
            synchronized (GlideUtil.class) {
                if (mInstance == null) {
                    mInstance = new GlideUtil();
                }
            }
        }
        return mInstance;
    }

    /**
     * 加载用户头像
     *
     * @param context
     * @param imageView
     * @param url
     */
    public void loadUserPhoto(final Context context, final ImageView imageView, String url) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .error(R.mipmap.ic_user_portrait_default)
                .placeholder(R.mipmap.ic_user_portrait_default)
                .transform(
                        new CenterCrop(context),
                        new RoundedCornersTransformation(context, Contants.CORNER_RADIUS, 0))
                .listener(new RequestListener<String, Bitmap>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                        if (e != null && e.toString().contains("403")) {
                            Glide.with(context).load(R.mipmap.ic_illegal_small).centerCrop().into(imageView);
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(imageView);
    }


    /**
     * 加载圆形图片
     *
     * @param context
     * @param imageView
     * @param url
     */
    public void loadRound(Context context, ImageView imageView, String url) {
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.ic_user_portrait_default)
                .error(R.mipmap.ic_user_portrait_default)
                .bitmapTransform(new CropCircleTransformation(context))
                .into(imageView);
    }


    /**
     * 常规加载图片
     *
     * @param context
     * @param imageView 图片容器
     * @param imgUrl    图片地址
     */
    public void loadImage(Context context, ImageView imageView, String imgUrl, int loginimg, int errorimg) {
        Glide.with(context)
                .load(imgUrl)
                .crossFade()
                .placeholder(loginimg)
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .error(errorimg)
                .into(imageView);
    }

    /**
     * 常规加载图片  yas
     *
     * @param context
     * @param imageView 图片容器
     * @param imgUrl    图片地址
     */
    public void loadImage2(Context context, ImageView imageView, String imgUrl, int loginimg) {
        Glide.with(context)
                .load(imgUrl)
                .crossFade()
                .placeholder(loginimg)
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .into(imageView);
    }

    /**
     * 加载冲满整个控件的图片  并且压缩
     *
     * @param context
     * @param imageView
     * @param imgUrl
     */
    public void bannerView(Context context, ImageView imageView, String imgUrl) {
        Glide.with(context)
                .load(imgUrl)
                .crossFade()
                .priority(Priority.HIGH) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .thumbnail(Contants.THUMB_SIZE)
                .centerCrop()
                .into(imageView);
    }


    public void loadMp4(Context context, ImageView imageView, String imgUrl, int place) {
        Glide.with(context)
                .load(imgUrl)
                .placeholder(place)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(Contants.THUMB_SIZE)
                .error(place)
                .into(imageView);
    }


    /**
     * 加载缩略图
     *
     * @param context
     * @param imageView 图片容器
     * @param imgUrl    图片地址
     */
    public void loadThumbnailImage(Context context, ImageView imageView, int imgUrl) {
        Glide.with(context)
                .load(imgUrl)
                .crossFade()
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .thumbnail(Contants.THUMB_SIZE)
                .centerCrop()
                .into(imageView);
    }

    /**
     * 拉升并压缩
     *
     * @param context
     * @param imageView
     * @param imgUrl
     * @param loadingImage
     */
    public void loadCropThumbnailImage2(Context context, ImageView imageView, String imgUrl, int loadingImage) {
        Glide.with(context)
                .load(imgUrl)
                .error(loadingImage)
                .crossFade()
                .placeholder(loadingImage)
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .thumbnail(Contants.THUMB_SIZE)
                .centerCrop()
                .into(imageView);
    }

    public void loadCropThumbnailImage3(Context context, ImageView imageView, String imgUrl, int loadingImage) {
        Glide.with(context)
                .load(imgUrl)
                .error(loadingImage)
                .placeholder(loadingImage)
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .thumbnail(Contants.THUMB_SIZE)
                .centerCrop()
                .into(imageView);
    }


    public void loadNoCaChe(Context context, ImageView imageView, String imgUrl, int errorImg) {
        Glide.with(context)
                .load(imgUrl)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(true)
                .thumbnail(0.5f)
                .error(errorImg)
                .placeholder(errorImg)
                .centerCrop()
                .into(imageView);
    }


    /**
     * 加载圆角图片
     *
     * @param context
     * @param imageView
     * @param imgUrl
     */
    public void loadCornerImage(Context context, ImageView imageView, String imgUrl, int errorimg) {
        Glide.with(context)
                .load(imgUrl)
                .dontAnimate()
                .priority(Priority.HIGH) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .error(errorimg)
                .placeholder(errorimg)
                .thumbnail(Contants.THUMB_SIZE)
                .bitmapTransform(
                        new CenterCrop(context),
                        new RoundedCornersTransformation(context, Contants.CORNER_RADIUS, 0))
                .into(imageView);
    }


    /**
     * 加载gif
     *
     * @param context
     * @param imageView
     * @param imgUrl
     */
    public void loadGifImage(Context context, ImageView imageView, String imgUrl) {
        Glide.with(context)
                .load(imgUrl)
                .asGif()
                .crossFade()
                .priority(Priority.NORMAL) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .into(imageView);
    }

    /**
     * 加载gif的缩略图
     *
     * @param context
     * @param imageView
     * @param imgUrl
     */
    public void loadGifThumbnailImage(Context context, ImageView imageView, int imgUrl) {
        Glide.with(context)
                .load(imgUrl)
                .asGif()
                .crossFade()
                .priority(Priority.HIGH) //下载的优先级
                .diskCacheStrategy(DiskCacheStrategy.ALL) //缓存策略
                .thumbnail(Contants.THUMB_SIZE)
                .into(imageView);
    }


    /**
     * 加载水印图
     *
     * @param context
     * @param url
     * @param imageView
     */
    public void loadWatermark(final Context context, String url, final ImageView imageView) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .placeholder(R.mipmap.img_car_big_perch)
                .transform(new WatermarkTransformation(context))
                .listener(new RequestListener<String, Bitmap>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                        Log.i(TAG, "onException: " + e.getMessage());
                        if (e != null && e.toString().contains("403")) {
                            Glide.with(context).load(R.mipmap.ic_illegal_big).into(imageView);
                            return true;
                        }

                        if (e != null && e.toString().contains("424")) {
                            Glide.with(context).load(R.mipmap.img_car_big_perch).into(imageView);
                            return true;
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(imageView);
    }

    /**
     * 恢复请求，一般在停止滚动的时候
     *
     * @param context
     */
    public void resumeRequests(Context context) {
        Glide.with(context).resumeRequests();
    }

    /**
     * 暂停请求 正在滚动的时候
     *
     * @param context
     */
    public void pauseRequests(Context context) {
        Glide.with(context).pauseRequests();
    }

    /**
     * 清除磁盘缓存
     *
     * @param context
     */
    public void clearDiskCache(final Context context) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Glide.get(context).clearDiskCache();//清理磁盘缓存 需要在子线程中执行
            }
        }).start();
    }

    /**
     * 清除内存缓存
     *
     * @param context
     */
    public void clearMemory(Context context) {
        Glide.get(context).clearMemory();//清理内存缓存  可以在UI主线程中进行
    }

    /**
     * 常量
     */
    static class Contants {
        public static final int BLUR_VALUE = 10; //模糊
        public static final int CORNER_RADIUS = 6; //圆角
        public static final float THUMB_SIZE = 0.5f; //0-1之间  10%原图的大小
    }

}
